package com.herui.ota.controller;

import com.herui.ota.config.ControllerNames;
import com.herui.ota.service.FileSrcService;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import io.swagger.annotations.Api;
import org.shan.spring.base.BaseController;
import org.shan.spring.base.annotations.SysLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by amanda.shan on 2020/12/18.
 */
@Api(value = ControllerNames.FileName + "接口", tags = {ControllerNames.FileName + "接口"})
@SysLog(title = ControllerNames.FileName)
@RestController
@RequestMapping("/file")
public class FileController extends BaseController {

    @Autowired
    private FileSrcService fileService;

    @GetMapping("")
    public void download(String filename, Integer skip, HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileService.download(filename, skip, request, response);
    }

    @GetMapping("/base64")
    public ResponseData<String> download(String filename) throws IOException {
        String s = fileService.download(filename);
        return result(s);
    }

    @PostMapping("")
    public Response uploadHandler(@RequestParam("file") MultipartFile file,
                                  @RequestParam("filename") String filename) {
        fileService.upload(filename, file);
        return result();
    }

    @GetMapping("/start")
    public ResponseData start(@RequestParam("filename") String filename) {
        fileService.start(filename);
        return result(new Object());
    }

    @GetMapping("/end")
    public ResponseData start(@RequestParam("taskId") Integer taskId) {
        fileService.end(taskId);
        return result(new Object());
    }
}
